package ru.susu.algebra.partition.vectex;

import ru.susu.algebra.pair.Pair;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.partition.operation.Partition2PairsOperation;
import ru.susu.algebra.partition.operation.Partition2VertexOperation;
import ru.susu.algebra.partition.polyhedron.checkers.SimplexMethodVertexChecker;
import ru.susu.algebra.util.LRUHashMap;

/**
 * Кэш должен вмещать все вершины для одного N, иначе начнется постоянная перестройка всех вершин в симплекс методе {@link SimplexMethodVertexChecker}
 *
 * @author akargapolov
 * @since: 12.03.2010
 */
public class Partition2VertexCache
{
	private static LRUHashMap<Partition, Integer[]> _partition2Vertex = new LRUHashMap<Partition, Integer[]>(200000);

	public static Integer[] getVertex(Partition partition)
	{
		Integer[] result = _partition2Vertex.get(partition);
		if (result != null)
			return result;
		result = Partition2VertexOperation.INSTANCE.performOperation(partition);
		_partition2Vertex.put(partition, result);
		return result;
	}

	private static LRUHashMap<Partition, Integer[]> _partition2Array = new LRUHashMap<Partition, Integer[]>(200000);

	public static Integer[] getArray(Partition partition)
	{
		Integer[] result = _partition2Array.get(partition);
		if (result != null)
			return result;
		result = partition.getArrayRepresentation();
		_partition2Array.put(partition, result);
		return result;
	}

	private static LRUHashMap<Partition, Pair<Integer, Integer>[]> _partition2Pairs = new LRUHashMap<Partition, Pair<Integer, Integer>[]>(200000);

	public static Pair<Integer, Integer>[] getPairs(Partition partition)
	{
		Pair<Integer, Integer>[] result = _partition2Pairs.get(partition);
		if (result != null)
			return result;
		result = Partition2PairsOperation.INSTANCE.performOperation(partition);
		_partition2Pairs.put(partition, result);
		return result;
	}
}
